package cn.edu.tjut.control;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import cn.edu.tjut.bean.ExpclassBean;
import cn.edu.tjut.bean.ReportBean;
import cn.edu.tjut.bean.StudentBean;
import cn.edu.tjut.myUtils.MyUtils;
import cn.edu.tjut.seriver.ExpclassService;
import cn.edu.tjut.seriver.StuService;
import cn.edu.tjut.seriver.impl.ReportInfoService;

@Controller
public class ExpClassInfoControl {
	// *************实验课程信息管理**************************************//
	@Resource
	private ExpclassService expService;
	@Resource
	private StuService stuService;
	@Resource
	private ReportInfoService repService;

	// 跳转教师发布实验课程界面
	@RequestMapping(value = "/insertExpclass.do")
	public String insertTeaInfo0() {
		return "expclassManager/TAddExp";
	}

	// 新发布实验课程界面逻辑处理
	@RequestMapping(value = "/Expdetails.do")
	public String insertTeaInfo1(ExpclassBean expB, HttpSession session, Model model,
			@RequestParam("file") CommonsMultipartFile file) throws Exception {
		String fileName = file.getOriginalFilename();
		if (fileName != "" || fileName != null) {
			expB.setExpWord("fileUpload/" + fileName);
		}
		String path = "D:\\lanqiaoJava\\lanqiaoworkspace\\gra_Project\\src\\main\\webapp\\fileUpload\\" + fileName;
		File newFile = new File(path);
		if (!newFile.exists()) {
			file.transferTo(newFile);
		}
		int i = expService.insertExpInfo(expB);
		if (i > 0) {
			List<ExpclassBean> lstExp = expService.selectAllexpclass();
			if (lstExp != null) {
				session.setAttribute("lstExp", lstExp);
				return "expclassManager/TMyExp";
			} else {
				session.setAttribute("Prompt", "暂时没有您的实验课程信息");
				return "expclassManager/TMyExp";
			}
		} else {
			session.setAttribute("Prompt", "添加实验课程信息失败！");
			return "expclassManager/TMyExp";
		}
	}

	// 查询所有实验课程信息
	@RequestMapping(value = "/selectExp.do")
	public String selectAllExpInfo(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
		String teaName = request.getParameter("teaname");
		List<ExpclassBean> lstExp = expService.selectByteaName(teaName);
		if (lstExp != null) {
			session.setAttribute("lstExp", lstExp);
			return "expclassManager/TMyExp";
		} else {
			session.setAttribute("Prompt", "暂时没有您的实验课程信息");
			return "expclassManager/TMyExp";
		}
	}

	// 查看实验课程信 详情
	@RequestMapping(value = "/lookExpInfo.do")
	public String lookExpInfo(ExpclassBean expB, HttpServletRequest request, HttpServletResponse response) {
		String expIndex = request.getParameter("ope");// 获取实验课程的主键
		expB = expService.selectByexpIndex(expIndex);// 根据主键查询实验课程信息
		request.setAttribute("expB", expB);// 设置缓存
		String opt = request.getParameter("opt");
		if (opt.equals("0")) {
			return "expclassManager/TAddExpReadonly";// 跳转查看详情页面
		} else if (opt.equals("2")) {
			request.setAttribute("stucla", expB.getStuClass());// 设置班级缓存
			return "expclassManager/TAddExpUpdate";// 跳转修改页面
		} else {
			return "reportManager/TLookRrport";// 跳转到教师查询学生实验报告界面
		}

	}

	// 修改实验课程信息
	@RequestMapping(value = "/updateExpInfo.do")
	public String updateExpInfo(ExpclassBean expB, HttpSession session, Model model,
			@RequestParam("file") CommonsMultipartFile file) throws Exception {
		String fileName = file.getOriginalFilename();
		if (fileName != "") {
			expB.setExpWord("fileUpload/" + fileName);
			String path = "D:\\lanqiaoJava\\lanqiaoworkspace\\gra_Project\\src\\main\\webapp\\fileUpload\\" + fileName;
			File newFile = new File(path);
			if (!newFile.exists()) {
				file.transferTo(newFile);
			}
		}
		int i = expService.updateByexpIndex(expB);
		if (i > 0) {
			List<ExpclassBean> lstExp = expService.selectAllexpclass();
			if (lstExp != null) {
				session.setAttribute("lstExp", lstExp);
				return "expclassManager/TMyExp";
			} else {
				session.setAttribute("Prompt", "暂时没有您的实验课程信息");
				return "expclassManager/TMyExp";
			}
		} else {
			session.setAttribute("Prompt", "修改实验课程信息失败！");
			return "expclassManager/TMyExp";
		}
	}

	// 删除实验课程信息
	@RequestMapping(value = "/deleteExpInfo.do")
	public String deleteExpInfo(ExpclassBean expB, HttpServletRequest request, HttpServletResponse response,
			HttpSession session) {
		String expIndex = request.getParameter("ope");// 获取实验课程的主键
		expB.setExpIndex(MyUtils.obj2Integer(expIndex));
		expService.deleteByexpIndex(expB);
		List<ExpclassBean> lstExp = expService.selectAllexpclass();
		if (lstExp != null) {
			session.setAttribute("lstExp", lstExp);
			return "expclassManager/TMyExp";
		} else {
			session.setAttribute("Prompt", "暂时没有您的实验课程信息");
			return "expclassManager/TMyExp";
		}
	}

	@RequestMapping(value = "/selectExpById.do")
	public String selectExpById(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
		Long expid = MyUtils.obj2Long(request.getParameter("expid"));
		ExpclassBean expB = expService.selectByexpId(expid);
		request.setAttribute("expB", expB);
		// ************获取学生学号并放入缓存******************************//
		Long stuid = MyUtils.obj2Long(request.getParameter("stuid"));
		StudentBean stuB = stuService.selectBystuId(MyUtils.obj2Long(stuid));
		// trueClass(stuB);// 转换班级显示格式
		request.setAttribute("stuB", stuB);
		// ******************************************//
		String opt = request.getParameter("opt");
		if (opt.equals("0")) {
			// 学生查看实验课程详情--根据课程ID查询实验课程信息--跳转新的ExpReadonly界面
			return "expclassManager/SAddExpReadonly";// 跳转查看详情页面
		} else {
			ReportBean repB = repService.selectByexpId(expid);
			if (repB == null) {
				// 跳转学生填写实验报告界面
				// 填写实验报告界面需要预读学生信息，所以根据传来的学号stuid查询学生的信息并放入缓存
				// Long stuid = MyUtils.obj2Long(request.getParameter("stuid"));
				// StudentBean stuB =
				// stuService.selectBystuId(MyUtils.obj2Long(stuid));
				trueClass(stuB);// 转换班级显示格式
				request.setAttribute("stuB", stuB);
				return "reportManager/addExpReport";// 跳转填写实验报告界面
			} else {
				request.setAttribute("repB", repB);// 设置缓存
				return "reportManager/ExpReportUpdate";// 跳转修改页面
			}
		}
	}

	// 转换学生的班级（代号转换为文字）
	public void trueClass(StudentBean stuB) {
		if (stuB.getStuClass().equals("10")) {
			stuB.setStuClass("电信1班");
		} else if (stuB.getStuClass().equals("11")) {
			stuB.setStuClass("电信2班");
		} else if (stuB.getStuClass().equals("12")) {
			stuB.setStuClass("电科1班");
		} else if (stuB.getStuClass().equals("13")) {
			stuB.setStuClass("电科2班");
		} else if (stuB.getStuClass().equals("14")) {
			stuB.setStuClass("电科3班");
		} else if (stuB.getStuClass().equals("15")) {
			stuB.setStuClass("信息1班");
		} else if (stuB.getStuClass().equals("16")) {
			stuB.setStuClass("信息2班");
		} else if (stuB.getStuClass().equals("17")) {
			stuB.setStuClass("光电1班");
		} else if (stuB.getStuClass().equals("18")) {
			stuB.setStuClass("光电2班");
		} else if (stuB.getStuClass().equals("19")) {
			stuB.setStuClass("集成1班");
		} else if (stuB.getStuClass().equals("20")) {
			stuB.setStuClass("集成2班");
		}
	}

	// 学生查看自己的实验课程
	@RequestMapping(value = "/stuSelectExp.do")
	public String stuSelectExp(HttpSession session, HttpServletRequest request, HttpServletResponse response) {
		String stuclass = request.getParameter("stuclass");
		String stuid = request.getParameter("stuid");
		// 根据学生学号查询该学生信息
		StudentBean stuB = stuService.selectBystuId(MyUtils.obj2Long(stuid));
		List<ExpclassBean> lstExp = expService.selectBystuClass(stuclass);
		if (lstExp != null) {
			session.setAttribute("lstExp", lstExp);
			request.setAttribute("stuB", stuB);
			return "expclassManager/SMyExp";
		} else {
			session.setAttribute("Prompt", "暂时没有您的实验课程信息");
			return "expclassManager/SMyExp";
		}
	}
}
